/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package analysis;

import java.util.Hashtable;

import node.*;


/**
 *
 * @author zgredzik
 */
public class SemanticAnalyser extends DepthFirstAdapter {
    // storing identifiers
    private Hashtable htSymbolTable = new Hashtable();
    
    // check for redefinition of identifiers
    @Override
    public void caseAIdentifierDirectDeclarator(AIdentifierDirectDeclarator node)
    {
        TIdentifier identifier = node.getIdentifier();
        String key = identifier.getText();
        
        //if found, report an error
        if(htSymbolTable.containsKey(key))
        {
            System.out.println("Error: [" + identifier.getLine() + "," + identifier.getPos() + "] Identifier already defined.");
            System.exit(0);
        }
        else
        {
            htSymbolTable.put(key, key);
        }
    }
    @Override
    public void caseAIdentifierArrayDeclarator(AIdentifierArrayDeclarator node)
    {
        TIdentifier identifier = node.getIdentifier();
        String key = identifier.getText();
        
        //if found, report an error
        if(htSymbolTable.containsKey(key))
        {
            System.out.println("Error: [" + identifier.getLine() + "," + identifier.getPos() + "] Identifier already defined.");
            System.exit(0);
        }
        else
        {
            htSymbolTable.put(key, key);
        }
    }
    @Override
    public void caseAIdentifierDirectFunctionDeclarator(AIdentifierDirectFunctionDeclarator node)
    {
        TIdentifier identifier = node.getIdentifier();
        String key = identifier.getText();
        
        //if found, report an error
        if(htSymbolTable.containsKey(key))
        {
            System.out.println("Error: [" + identifier.getLine() + "," + identifier.getPos() + "] Identifier already defined.");
            System.exit(0);
        }
        else
        {
            htSymbolTable.put(key, key);
        }
    }
    // check if the identifier has been declared before referring to it
    @Override
    public void caseAIdentifierVarname(AIdentifierVarname node)
    {
        TIdentifier identifier = node.getIdentifier();
        String key = identifier.getText();
        
        //if found, report an error
        if(!htSymbolTable.containsKey(key))
        {
            System.out.println("Error: [" + identifier.getLine() + "," + identifier.getPos() + "] Reference to an undeclared identifier.");
            System.exit(0);
        }
    }
    @Override
    public void caseAArrayref(AArrayref node)
    {
        TIdentifier identifier = node.getIdentifier();
        String key = identifier.getText();
        
        //if found, report an error
        if(!htSymbolTable.containsKey(key))
        {
            System.out.println("Error: [" + identifier.getLine() + "," + identifier.getPos() + "] Reference to an undeclared identifier.");
            System.exit(0);
        }
    }
    @Override
    public void caseADirectCompref(ADirectCompref node)
    {
        TIdentifier identifier = node.getIdentifier();
        String key = identifier.getText();
        
        //if found, report an error
        if(!htSymbolTable.containsKey(key))
        {
            System.out.println("Error: [" + identifier.getLine() + "," + identifier.getPos() + "] Reference to an undeclared identifier.");
            System.exit(0);
        }
    }
}
